package com.pktechan.api.tradeOrderStatus;


import com.pktechan.api.entity.TradeOrder;
import com.pktechan.api.enumeration.ChangeTradeOrderStatusEnum;
import com.pktechan.api.enumeration.TradeOrderStatusEnum;
import com.pktechan.api.exception.CustomException;
import com.pktechan.api.sendMiniMessage.BaseSendMiniMessage;
import com.pktechan.api.sendMiniMessage.SendMiniMessageStrategy;
import com.pktechan.api.sendMiniMessage.bean.FactoryOrderTransferConfirmMessage;
import com.pktechan.api.sendMiniMessage.enumration.SendMiniMessageEnum;
import com.pktechan.api.service.SendMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: cloudfloating
 * @description: 转让订单
 * @author: qx.zhang
 * @create: 2021-03-15 11:27
 **/
@Component("transferOrder")
@Transactional
public class TransferOrder extends BaseChangeTradeOrderState implements ChangeTradeOrderState {

    private final static Logger logger = LoggerFactory.getLogger(TransferOrder.class);

    @Autowired
    private SendMessageService sendMessageService;

    /**
     * 获取实现类对应的行为枚举
     *
     * @return
     */
    @Override
    public ChangeTradeOrderStatusEnum getMappingEnum() {
        return ChangeTradeOrderStatusEnum.TRANSFER_ORDER;
    }

    /**
     * 订单状态改变
     *
     * @param tradeOrderId
     * @param stateOld
     * @param stateCur
     * @param custId
     * @param exeRemark
     */
    @Override
    public void doChange(Integer tradeOrderId, Integer stateOld, Integer stateCur, Integer custId, String exeRemark) throws CustomException {
        TradeOrder tradeOrder = super.tradeOrderDataValidation(tradeOrderId);
        if (!TradeOrderStatusEnum.ORDERS_TO_BE_RECEIVED.getCode().equals(tradeOrder.getStatus())) {
            logger.debug("订单ID为" + tradeOrderId + "对应的订单状态为" + TradeOrderStatusEnum.getNameByCode(tradeOrder.getStatus()) + "，不可转让");
            throw new CustomException("当前订单状态为" + TradeOrderStatusEnum.getNameByCode(tradeOrder.getStatus()) + "，不可转让");
        }
        logger.debug("转让订单" + "，订单ID：" + tradeOrderId + "，变更前状态：" + TradeOrderStatusEnum.ORDERS_TO_BE_RECEIVED.getName() + "，变更后状态：" + TradeOrderStatusEnum.ORDERS_TO_BE_RECEIVED.getName()
                + "，操作人角色ID：" + custId + "，操作说明：" + exeRemark);
        //插入状态变化记录
        super.addTradeOrderStateFlow(tradeOrderId, stateOld, stateCur, custId, exeRemark);
        //更改订单状态
        super.updateTradeOrder(tradeOrderId, TradeOrderStatusEnum.ORDERS_TO_BE_RECEIVED.getCode());
        //厂家接收-配送商-订单转让确认-订单配送通知
        FactoryOrderTransferConfirmMessage factoryOrderTransferConfirmMessage = sendMessageService.getFactoryOrderTransferConfirmMessage(tradeOrderId);
        SendMiniMessageStrategy FactoryOrderTransferStrategy = BaseSendMiniMessage.getSendMiniMessage(SendMiniMessageEnum.FACTORY_ORDER_TRANSFER_CONFIRM);
        FactoryOrderTransferStrategy.sendMiniMessageToActiveMq(factoryOrderTransferConfirmMessage);

    }
}
